package com.atguigu.ucenter.controller;

import com.atguigu.commonutils.result.R;
import com.atguigu.ucenter.entity.UcenterMember;
import com.atguigu.ucenter.service.UcenterMemberService;
import com.atguigu.ucenter.vo.LoginVo;
import com.atguigu.ucenter.vo.RegisterVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * @author kabuda
 * @create 2023-05-12 23:09
 */
@RestController
@RequestMapping("eduucenter")
public class MemberApiController {
    @Autowired
    private UcenterMemberService memberService;
    @PostMapping("login")
    public R login(@RequestBody LoginVo loginVo){
        String token=memberService.login(loginVo);
        return R.ok().data("token",token);
    }
    @PostMapping("register")
    public R registerVo(@RequestBody RegisterVo registerVo){
        memberService.registerVo(registerVo);
        return R.ok();
    }
    //根据token获取用户信息
    @GetMapping("getCurrentMember")
    public R getCurrentMember(HttpServletRequest request){
        String token = request.getHeader("token");
        UcenterMember member=memberService.getMemberByToken(token);
        return R.ok().data("member", member);
    }
    @GetMapping("getMember")
    public UcenterMember getMember(@RequestParam("memberId") String memberId){
        UcenterMember member = this.memberService.getById(memberId);
        return member;
    }

    /**
     * 获得用户某日注册人数
     * @param date
     * @return
     */
    @GetMapping("registerCount")
    public Integer registerCount(@RequestParam("date") String date){
     Integer count =this.memberService.registerCount(date);
     return count;
    }
}

